/**
 * Created by sxx on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";

jQuery(function () {
    App.init();
    let token = sessionStorage.getItem('supplier_token');
    let splrId = sessionStorage.getItem('splrId');
    var projId = getArgs().id;

    $('.J_return').click(function(){
        window.history.back();
    });

    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/bidProjOnMngForSplr/viewProjDtl/'+projId,
        type: 'get',
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,
            'login_type': 'splr'
        },
        success: function(resp){
            if (!resp.success) {
                console.log(resp.msgCod+' '+resp.msgInf);
                return;
            }
            var projqotCntTyp='非实时报价',bidRngTyp='非定向招标';
            if (resp.data.bidProjOn.qotCntTyp == 2) {
                projqotCntTyp='实时报价';
            }
            if (resp.data.bidProjOn.bidRngTyp == 1) {
                bidRngTyp='定向招标';
            }

            var bidOpenTim='',bidEndTim='',projMemo='',splrNames='';
            if (resp.data.bidProjOn.bidOpenTim != null&&resp.data.bidProjOn.bidOpenTim !='') {
                var bidOpenTim = (new Date(resp.data.bidProjOn.bidOpenTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.bidProjOn.bidEndTim != 'null'&&resp.data.bidProjOn.bidEndTim !='') {
                var bidEndTim = (new Date(resp.data.bidProjOn.bidEndTim)).Format("yyyy-MM-dd hh:mm:ss");
            }
            if (resp.data.bidProjOn.projMemo != null&&resp.data.bidProjOn.projMemo !='') {
                var projMemo = resp.data.bidProjOn.projMemo;
            }
            if(resp.data.splrs){
                $.each(resp.data.splrs,function(index,item){
                    splrNames += item.splrNam + " ";
                })
            }
            console.log(resp);
            var matTyp = resp.data.bidProjOn.matTyp;
            if(resp.data.matTypDesc!=null && resp.data.matTypDesc!=''){
                matTyp = resp.data.matTypDesc;
            }

            var grdRulList = {"0":"最低单价中标","1":"专家评标","2":"最低总价评标"};
            if (typeof(resp.data.bidProjOn.grdRul) == 'undefined' || resp.data.bidProjOn.grdRul == null
                || resp.data.bidProjOn.grdRul == '' || resp.data.bidProjOn.grdRul == '0') {
                resp.data.bidProjOn.grdRulFmt = '最低单价中标';
            } else {
                resp.data.bidProjOn.grdRulFmt = grdRulList[resp.data.bidProjOn.grdRul];
            }
            if(typeof(resp.data.bidProjOn.projSupv) == 'undefined' || resp.data.bidProjOn.projSupv == null){
                resp.data.bidProjOn.projSupv = '';
            }
            if(typeof(resp.data.bidProjOn.projSupvTel) == 'undefined' || resp.data.bidProjOn.projSupvTel == null){
                resp.data.bidProjOn.projSupvTel = '';
            }
            jQuery('.basicInfo').append('<div class="portlet-body"><table class="table table-striped table-bordered"><tbody><tr>' +
                '<td width="16%">项目名称：</td><td colspan="2">'+resp.data.bidProjOn.projNam+'</td> ' +
                '<td width="16%">项目编号：</td> <td colspan="2">'+resp.data.bidProjOn.projNbr+'</td></tr> <tr>' +
                '<td width="16%">物料品类：</td><td colspan="2">'+matTyp+'</td>' +
                '<td width="16%">报价次数类型：</td><td colspan="2">'+projqotCntTyp+'</td></tr> <tr>' +
                '<td width="16%">招标单位：</td><td colspan="2">'+resp.data.bidProjOn.bidDptNam+'</td>' +
                '<td width="16%">招标单位地址：</td><td colspan="2">'+resp.data.bidProjOn.bidDptAddr+'</td></tr><tr>' +
                '<td width="16%">投标保证金（元）：</td><td colspan="2">'+resp.data.bidProjOn.bidDpst+'</td>' +
                '<td width="16%">评标规则：</td><td colspan="2">'+resp.data.bidProjOn.grdRulFmt+'</td></tr><tr>' +
                '<td width="16%">联系人：</td><td colspan="2">'+resp.data.bidProjOn.ctct+'</td>' +
                '<td width="16%">联系电话：</td><td  colspan="2">'+resp.data.bidProjOn.ctctTel+'</td></tr><tr>' +
                '<td width="16%">监标人：</td><td colspan="2">'+resp.data.bidProjOn.projSupv+'</td>' +
                '<td width="16%">监标人联系电话：</td><td  colspan="2">'+resp.data.bidProjOn.projSupvTel+'</td></tr><tr>' +
                '<td width="16%">投标开标时间：</td><td colspan="2">'+bidOpenTim+'</td>' +
                '<td width="16%">投标截止时间：</td><td colspan="2">'+bidEndTim+'</td></tr><tr>' +
                '<td>招标范围类型：</td><td colspan="2">'+bidRngTyp+'</td>' +
                '<td width="16%">指定招标供应商：</td><td colspan="2">'+splrNames+'</td></tr><tr>' +
                '<td>付款方式及其他：</td><td colspan="5">'+projMemo+'</td></tr></tbody></table></div>');
			if(resp.data.matDtls.length ==0){
				jQuery('#emptyData').hide();
			}else{
				jQuery('#emptyData').show();
			}

            var atches = resp.data.atches;
            if ((atches!=null && atches!='undefined') && atches.length > 0){
                for (var i= 0;i<atches.length;i++) {
                    jQuery('#tab_3').append('<div class="controls atchDiv"><span src="'+atches[i].atchUrl+'">'+atches[i].atchNam+'</span>' +
                        '&emsp;<a target="_blank" href="'+HOST_FILE+atches[i].atchUrl+'">查看</a></div>');
                }
            }

            for(var i = 0;i< resp.data.matDtls.length;i++) {
            	if(resp.data.matDtls[i].memo == null){
            		resp.data.matDtls[i].memo = ""; 
            	}
            	if(resp.data.matDtls[i].techServ == null){
                    resp.data.matDtls[i].techServ = "";
                }
                resp.data.matDtls[i].dlvDte = resp.data.matDtls[i].dlvDte ? (new Date(resp.data.matDtls[i].dlvDte)).Format("yyyy-MM-dd") : '';
//				console.log(resp.data.matDtls[i])
                jQuery('#new_basicInfo').after('<tr><td style="text-align:center;width: 18% !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].matNam+'</td>'+
                	'<td style="text-align:center;width: 11% !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].pchsNum+'</td>'+
                	'<td style="text-align:center;width: 8% !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].matUnt+'</td>'+
                    '<td style="text-align:center;width: 8% !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].dlvAdr+'</td>'+
                    '<td style="text-align:center;width: 8% !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].dlvDte+'</td>'+
                	'<td style="width: 25% !important;word-wrap:break-word !important;word-break:break-all !important;overflow:hidden !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].techServ+'</td>'+
                	'<td style="width: 12% !important;word-wrap:break-word !important;word-break:break-all !important;overflow:hidden !important;vertical-align:middle;display:table-cell;">'+resp.data.matDtls[i].memo+'</td></tr>'
                );
            }
        },
        error:function(err){
            console.log(err);
        }
    });

    // 获取在线答疑信息列表
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/splrOnline/splrAnsrList',
        type: 'get',
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,
            'login_type': 'splr',
            'projId':projId
        },
        success: function(resp){
            console.log(resp);
            // 显示公开内容
            $.each(resp.data.openContList,function(index,item){
                var nameFmt = item.queTime ? '提问' : '回答';
                var content = item.queTime ? item.queContent : item.ansrContent;
                var fromManager = item.queTime ? 0 : 1;
                var disClass = item.queTime ? "" : "font-color";
                var tmphtml = '<li class="detail" msgId="'+item.mid+'" fromManager="'+fromManager+'">' +
                    '<div class="nameFmt">'+nameFmt+' </div>' +
                    '<div class="msgContent"><span class="'+disClass+'">'+content+'</span></div></li>';
                $(".pubMsg .msgList").append(tmphtml);
            })

            // 显示个人对话
            $.each(resp.data.dialogList,function(index,item){
                var createTim = item.queTime ? item.queTime : item.ansrTime;
                createTim = createTim ? (new Date(createTim)).Format("yyyy-MM-dd hh:mm:ss") : '';
                var nameFmt = (item.queTime ? '我 ' : '管理员 ') + createTim;
                var content = item.queTime ? item.queContent : item.ansrContent;
                var fromManager = item.queTime ? 0 : 1;
                var disClass = item.queTime ? "bg-green" : "";
                var tmphtml = '<li class="detail" msgId="'+item.mid+'" fromManager="'+fromManager+'">' +
                    '<div class="nameFmt">'+nameFmt+' </div>' +
                    '<div class="msgContent"><span class="'+disClass+'">'+content+'</span></div></li>';
                $(".personalMsg .msgList").append(tmphtml);
            });

        },
        error:function(err){
            console.log(err);
        }
    });

    // 提问
    $(".chat .reply").on("click",function(){
        var chatMsg = $(".chatMsg").val();
        var currentDate = new Date().Format("yyyy-MM-dd hh:mm:ss")
        if(splrId == ''|| chatMsg == ''){
            return false;
        }
        var data= {
            "content": chatMsg,
            "projId": projId,
            "splrId": splrId
        };
        $.ajax({
            url: HOST_ADDR+'/cpmp/splrOnline/que',
            type: 'post',
            dataType: 'json',
            contentType:"application/json",
            headers: {
                'access_token': token,
                'login_type': 'splr'
            },
            data:JSON.stringify(data),
            success: function(resp){
                if(resp.success){
                    var html='<li class="detail" msgId="" fromManager="0">'+
                        '<div class="nameFmt">我 '+currentDate+'</div>'+
                        '<div class="msgContent"><span class="bg-green">'+chatMsg+'</span>'+
                        '</li>';
                    $(".personalMsg .msgList").append(html);
                    $(".chatMsg").val('');
                    var scrolTop = $(".chatMsg").height()-$(".personalMsg").height();
                    scrolTop = scrolTop>0 ? scrolTop : 0;
                    $(".personalMsg").scrollTop(scrolTop);
                }else{
                    alert(resp.msgInf);
                }
                console.log(resp);
            },
            error:function(err){
                console.log(err);
            }
        });
    });





});

